﻿using Paas.Core.Approvals.Entities;
using Paas.Core.Common.Entities;
using Paas.Core.Const;
using Paas.Core.Const.Enums.Approvals;
using Paas.Core.Const.Enums.Common;
using Paas.Core.Sys.Entities;
using Microsoft.AspNetCore.Hosting;
using Basefr.Runtime.Helpers;
using System;
using System.Linq;

namespace Paas.Infrastructure.Data.EntityFrameworkCore
{
    public static class MainDbInitializer
    {
        public static Action<MainDbContext> Initializer(IWebHostEnvironment hostingEnvironment) => dbContext =>
        {
            if (!dbContext.Set<User>().Any())
            {
                //部门
                var organization = new Organization { Name = "默认组织", ParentId = null, Level = 0, IsPreDefined = true };
                dbContext.Set<Organization>().Add(organization);

                //角色
                var role = new Role { Name = Role.DefaultRoleName, IsActive = true, IsPreDefined = true };
                dbContext.Set<Role>().Add(role);

                //权限
                PermissionHelper.GetList<Permissions>().Select(p => p.Key).ToList().ForEach(permission =>
                {
                    dbContext.Set<RolePermission>().Add(new RolePermission { Role = role, Permission = permission, IsPreDefined = true });
                });

                //用户
                var salt = Guid.NewGuid().ToString("N");
                var user = new User
                {
                    UserName = User.DefaultAdminName,
                    Password = HashHelper.Hash(User.DefaultAdminPassword, salt),
                    PasswordSalt = salt,
                    Organization = organization,
                    Name = "管理员",
                    Phone = "12345678900",
                    Email = "email@email.com",
                    IsActive = true,
                    IsPreDefined = true,
                };
                dbContext.Set<User>().Add(user);

                //用户角色
                dbContext.Set<RoleUser>().Add(new RoleUser { User = user, Role = role, IsPreDefined = true });



                dbContext.SaveChanges();
            }
            else
            {
                var preDefinedRole = dbContext.Set<Role>().First(p => p.Name == Role.DefaultRoleName && p.IsPreDefined);
                var preDefinedRolePermissions = dbContext.Set<RolePermission>().Where(p => p.RoleId == preDefinedRole.Id).Select(p => p.Permission).ToList();

                PermissionHelper.GetList<Permissions>().Select(p => p.Key).ToList().ForEach(permission =>
                {
                    if (preDefinedRolePermissions.All(p => p != permission))
                    {
                        dbContext.Set<RolePermission>().Add(new RolePermission { Role = preDefinedRole, Permission = permission, IsPreDefined = true });
                    }
                });

                dbContext.SaveChanges();
            }

            var approveTemplateTypes = Enum.GetValues(typeof(EnumApproveType)).OfType<EnumApproveType>().Where(p => p != EnumApproveType.None).ToList();
            var existTemplates = dbContext.Set<ApproveTemplate>().ToList();
            if (existTemplates.Count < approveTemplateTypes.Count)
            {
                approveTemplateTypes.FindAll(type => existTemplates.All(et => et.Type != type)).ForEach(type =>
                {
                    dbContext.Set<ApproveTemplate>().Add(new ApproveTemplate { Type = type });

                    dbContext.SaveChanges();
                });
            }

            if (!dbContext.Set<Serial>().Any())
            {
                dbContext.Set<Serial>().Add(new Serial { ObjectType = EnumObjectType.None });
                dbContext.SaveChanges();
            }
        };
    }
}

